<template>
  <div style="height:6vh;background-image: linear-gradient(to right, #fffacd, #add8e6);padding: 10px 20px;">
    <span style="line-height: 60px;font-size: 20px;font-weight: bold">用户管理</span>
  </div>

  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="用户搜索" style="width: 270px;">
        <el-input placeholder="请输入用户名"
                  v-model="searchUserForm.username" @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="违规次数" style="width: 290px;">
        <el-input placeholder="请输入违规次数筛查"
                  v-model="searchUserForm.violationCount" @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" style="width: 290px;">
        <el-select placeholder="请选择用户状态"
                   v-model="searchUserForm.status" @change="loadUser">
          <el-option label="正常" value="normal"></el-option>
          <el-option label="封禁7天" value="ban_7"></el-option>
          <el-option label="封禁30天" value="ban_30"></el-option>
          <el-option label="禁言" value="mute"></el-option>
          <el-option label="永久封禁" value="ban_forever"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="loadUser">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 用户数据表格卡片 -->
  <el-card style="margin:20px;">
    <el-table :data="userArr">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="用户名" prop="username" align="center"></el-table-column>
      <el-table-column label="邮箱" prop="email" align="center"></el-table-column>
      <el-table-column label="违规次数" prop="violationCount" align="center"></el-table-column>
      <el-table-column label="详情" align="center">
        <template #default="scope">
          <el-button type="primary" link round size="small" :disabled="scope.row.level==40"
                     @click="details(scope.row.userId)">违规详情
          </el-button>
        </template>
      </el-table-column>
      <!-- 操作列 -->
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="openActionDialog(scope.row.userId)">处理</el-button>
        </template>
      </el-table-column>
      <!-- 用户状态列 -->
      <el-table-column label="用户状态" align="center">
        <template #default="scope">
          <span>{{ getStatusText(scope.row.status) }}</span>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 用户违规详细内容弹窗 -->
  <el-dialog
      v-model="dialogVisible"
      title="用户违规详细内容"
      width="50%">
    <el-table :data="violationDetails">
      <el-table-column label="违规时间" prop="violationTime" align="center"></el-table-column>
      <el-table-column label="违规原因" prop="reason" align="center"></el-table-column>
    </el-table>
  </el-dialog>

  <!-- 处理方式弹窗 -->
  <el-dialog
      v-model="actionDialogVisible"
      title="选择处理方式"
      width="30%"
      :before-close="handleActionClose">
    <el-form>
      <el-form-item label="处理方式">
        <el-select v-model="selectedAction" placeholder="请选择处理方式">
          <el-option label="封禁7天" value="ban_7"></el-option>
          <el-option label="封禁30天" value="ban_30"></el-option>
          <el-option label="禁言" value="mute"></el-option>
          <el-option label="永久封禁" value="ban_forever"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleActionClose">取消</el-button>
      <el-button type="primary" @click="applyAction">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { onMounted, ref } from "vue";
import axios from "axios";
import qs from "qs";
import { ElMessage } from "element-plus";

// 保存用户数据的数组
const userArr = ref([]);

// 搜索表单
const searchUserForm = ref({
  username: '',
  violationCount: '',
  status: '', // 用户状态
  userId: ''
});

// 控制弹窗
const dialogVisible = ref(false);
const actionDialogVisible = ref(false);

// 违规详细内容
const violationDetails = ref([]);

// 当前选择的用户ID
const currentUserId = ref(null);

// 选择的处理方式
const selectedAction = ref('');

// 加载用户数据
const loadUser = () => {
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL + '/p1/user/select?' + data)
      .then((response) => {
        if (response.data.code == 2000) {
          userArr.value = response.data.data;
          console.log(userArr.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      });
};

// 立即加载
onMounted(() => {
  loadUser();
});

// 重置搜索
const reset = () => {
  searchUserForm.value = {
    username: '',
    violationCount: '',
    status: '', // 重置用户状态
    userId: ''
  };
  loadUser();
};

// 弹窗关闭处理
const handleClose = () => {
  if (confirm('你确定要关闭弹窗吗？')) {
    dialogVisible.value = false;
  }
};

// 查看用户详细违规内容
const details = (userId) => {
  // 加载当前用户的违规详细内容
  axios.get(BASE_URL + '/p1/user/violation/details?userId=' + userId)
      .then((response) => {
        if (response.data.code == 2000) {
          violationDetails.value = response.data.data;
          dialogVisible.value = true;
        } else {
          ElMessage.error(response.data.msg);
        }
      });
};

// 打开处理方式弹窗
const openActionDialog = (userId) => {
  currentUserId.value = userId; // 设置当前用户ID
  actionDialogVisible.value = true; // 打开弹窗
};

// 处理方式弹窗关闭
const handleActionClose = () => {
  selectedAction.value = ''; // 清空选择的处理方式
  actionDialogVisible.value = false; // 关闭弹窗
};

// 应用处理方式
const applyAction = () => {
  if (!selectedAction.value) {
    ElMessage.warning('请选择处理方式！');
    return;
  }

  // 调用接口更新用户状态
  axios.post(`${BASE_URL}/p1/user/update/status/${currentUserId.value}/${selectedAction.value}`)
      .then((response) => {
        if (response.data.code === 2000) {
          ElMessage.success('处理成功！');
          loadUser(); // 刷新用户列表
          handleActionClose(); // 关闭弹窗
        } else {
          ElMessage.error(response.data.msg);
        }
      })
      .catch((error) => {
        ElMessage.error('处理失败，请稍后重试！');
        console.error('接口调用失败：', error);
      });
};

// 根据处理方式获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 'ban_7':
      return '封禁7天';
    case 'ban_30':
      return '封禁30天';
    case 'mute':
      return '禁言';
    case 'ban_forever':
      return '永久封禁';
    default:
      return '正常';
  }
};
</script>

<style scoped>
</style>